﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace OALPTool
{
    /* 调用方法
  dataGridViewEx1.TopRow.Cells[2].Text = "入库";
  dataGridViewEx1.TopRow.Cells[2].ColSpan = 2;
  dataGridViewEx1.TopRow.Cells[4].Text = "出库";
  dataGridViewEx1.TopRow.Cells[4].ColSpan = 2;*/
    public partial class DataGridViewEx : DataGridView
    {
        public TopRow topRow;

        public DataGridViewEx()
        {
            this.ColumnHeadersHeight = 50;
            this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
        }


        public TopRow TopRow
        {
            set { topRow = value; }
            get
            {
                if (topRow == null)
                {
                    topRow = new TopRow(this.ColumnCount);
                }
                return topRow;
            }
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == 0)
            {
                // int w = dataGridView1.HorizontalScrollingOffset + dataGridView1.TopLeftHeaderCell.Size.Width + dataGridView1.Columns[0].Width + 10; 
                Rectangle newRect = new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, e.CellBounds.Width - 4, e.CellBounds.Height - 4);
                Brush gridBrush = new SolidBrush(this.GridColor);
                Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor);
                Pen gridLinePen = new Pen(gridBrush);
                Brush headerTextBrush = new SolidBrush(ColumnHeadersDefaultCellStyle.ForeColor);
                // Erase the cell. 
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                // Draw the grid lines (only the right and bottom lines; DataGridView takes care of the others). 
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                if (e.ColumnIndex > -1 && TopRow != null && TopRow.Cells[e.ColumnIndex].ColSpan > 1)
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top + e.ClipBounds.Height / 2, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                }
                else
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                }

                // Draw the inset highlight box.  e.Graphics.DrawRectangle(Pens.Blue, newRect); 
                int scale = e.CellBounds.Height / 3;
                if (e.ColumnIndex > -1 && topRow.Cells[e.ColumnIndex].Text != null)
                {
                    scale = e.CellBounds.Height / 2;
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - e.CellBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom - e.CellBounds.Height / 2);
                }
                // Draw the text content of the cell, ignoring alignment. 
                if (e.Value != null)
                {
                    e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, headerTextBrush, e.CellBounds.X, e.CellBounds.Y + scale + 4, StringFormat.GenericDefault);
                }
                if (e.ColumnIndex > -1 && TopRow.Cells[e.ColumnIndex].Text != null)
                {
                    // Rectangle recCell = new Rectangle(e.CellBounds.X - 1 - topRow.Cells[e.ColumnIndex].SpanRowWith,  e.CellBounds.Y + 1, topRow.Cells[e.ColumnIndex].SpanRowWith,  e.CellBounds.Height / 2); 
                    Rectangle recCell = new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1 + e.CellBounds.Height / 5, e.CellBounds.Width, e.CellBounds.Height);
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Center;
                    e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, headerTextBrush, recCell, sf);
                }
                e.Handled = true;
            }
        }

        private void InitializeComponent()
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            //  
            // DataGridViewDoubleHeaders 
            //  
            this.ColumnHeadersHeight = 50;
            this.RowTemplate.Height = 23;
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }
    }

    public class TopRow
    {
        public TopRow(int columnCount)
        {
            cells = new Cell[columnCount];
            for (int i = 0; i < cells.Length; i++)
            {
                cells[i] = new Cell();
            }
        }

        Cell[] cells;

        public Cell[] Cells
        {
            get { return cells; }
            set { cells = value; }
        }
    }

    public class Cell
    {
        public Cell()
        {

        }

        string text;

        public string Text
        {
            get
            {
                if (text == null)
                {
                    return string.Empty;
                }
                else
                {
                    return text;
                }
            }
            set { text = value; }
        }
        int colSpan;

        public int ColSpan
        {
            get
            {
                if (colSpan == 0)
                {
                    return 1;
                }
                else
                {
                    return colSpan;
                }
            }
            set { colSpan = value; }
        }
        public override string ToString()
        {
            return Text;
        }
    }

}
